package com.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server01 {
    public static void main(String[] args) throws IOException {
        //监视9999端口
        ServerSocket serverSocket=new ServerSocket(9999);
        System.out.println("正在监听9999端口.....");
        //等待客户端连接,
       Socket socket= serverSocket.accept();
       //读取客户端发送的消息
        BufferedInputStream bis=new BufferedInputStream(socket.getInputStream());
        byte[]bytes=SocketTools.streamtoByteArray(bis);
        //将消息写到指定路径
        String path="D:\\javacode\\javacode\\J2024_11_21\\gold2.png";
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(path));
        bos.write(bytes);
        //回复消息给客户端
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        String message="长江!长江!我已收到消息";
        bufferedWriter.write(message);
        bufferedWriter.flush();
        //关闭流
        bos.close();
        bufferedWriter.close();
        bis.close();
        socket.close();
        serverSocket.close();

    }
}
